package com.guhappy.util;

import java.io.UnsupportedEncodingException;


import com.guhappy.model.user.WebUser;

public class GenerateLinkUtils{

	private static final String CHECK_CODE = "checkCode";

	public static String generateActivateLink(WebUser user) {
		return "http://99.237.162.241/GuHappy/activateAccount.action?id="
				+ user.getId() + "&" + CHECK_CODE + "="
				+ generateCheckcode(user);
	}

	public static String generateResetPwdLink(WebUser user) {
		return "http://99.237.162.241/GuHappy/resetPasswordUI?userName="
				+ user.getUserName()
				+ "&"
				+ CHECK_CODE
				+ "="
				+ generateCheckcode(user);
	}

	public static String generateCheckcode(WebUser user) {
		String userName = user.getUserName();
		String password = user.getUserPassword();
		return HashPassword(userName + ":" + password);
	}

	public static String HashPassword(String sPASSWORD) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest
					.getInstance("MD5");
			byte[] array = null;
			try {
				array = md.digest(sPASSWORD.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
}
